package com.godpaper.chessGameHall.command
{
	import com.adobe.cairngorm.commands.ICommand;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.godpaper.chessGameHall.events.UserCountChangeEvent;
	import com.godpaper.chessGameHall.model.RoomModelLocator;
	import com.godpaper.chessGameHall.util.StringUtil;
	
	import it.gotoandplay.smartfoxserver.SFSEvent;
	import it.gotoandplay.smartfoxserver.data.Room;
	
	import mx.rpc.IResponder;
	
	public class UserCountChangeCommand extends CommandBase implements ICommand, IResponder
	{
		private var userCountChangeEvent:UserCountChangeEvent;
		private var roomModel:RoomModelLocator = RoomModelLocator.getInstance();
		public function UserCountChangeCommand()
		{
			super();
		}
		
		override public function execute(event:CairngormEvent):void
		{
			userCountChangeEvent = UserCountChangeEvent(event);
			userCountChangeHandler( userCountChangeEvent.sfsEvent );
		}
		/*
		* Handle a count change in one room of the zone
		*/
		private function userCountChangeHandler(event:SFSEvent):void
		{
			var r:Room = event.params.room as Room;
			var id:int = r.getId();
			
			// Cycle through all rooms in the list and find the one that changed
			for each (var o:Object in roomModel.roomList )
			{
				if (o.data == id)
				{
					o.label = StringUtil.transactRoomLable( r );
					break;
				}
			}
		}
		
	}
}